package adv.i31.RobotMin3d;

import min3d.core.Object3dContainer;
import min3d.core.RendererActivity;
import min3d.parser.IParser;
import min3d.parser.Parser;
import min3d.vos.Light;
import min3d.vos.Number3d;
import adv.i31.RobotMin3d.BluetoothUi.Callback;
import android.graphics.Color;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends RendererActivity implements Callback
{
	private final Teachbox	teachbox		= new Teachbox();
	private final RV_M1		roboter			= new RV_M1();
	private final float		MAX_CAM_X		= 6f;
	private float			camDirection	= -.01f;
	private long			lastTimeMillis	= System.currentTimeMillis();
	private TextView		textView1;
	private TextView		textView2;
	private TextView		textView3;
	private TextView		textView4;
	private TextView		textView5;
	private TextView		textView6;
	private TextView	textView7;
	

	@Override
	public void onUpdateScene()
	{
		/* UI adjustments go here as this method is called by the UI thread */
		textView1.setText(String.format("alpha = %.2f", Math.toDegrees(roboter.alpha)));
		textView2.setText(String.format("beta = %.2f", Math.toDegrees(roboter.beta)));
		textView3.setText(String.format("gamma = %.2f", Math.toDegrees(roboter.gamma)));
		textView4.setText(String.format("zeta = %.2f", Math.toDegrees(roboter.zeta)));
		textView5.setText(String.format("x = %.2f", roboter.gotox));
		textView6.setText(String.format("y = %.2f", roboter.gotoy));
		textView7.setText(String.format("test = %.2f", roboter.test));
	}

	double rawx,rawy;
	private BluetoothUi	mBluetoothStuff;
	private Rvm1Connection	mRobotConnection;
	private ToggleButton	btnActive;
	
	@Override
	public boolean onTouchEvent(android.view.MotionEvent event)
	{
		switch (event.getAction()) {
			case MotionEvent.ACTION_MOVE:
				if (event.getHistorySize() > 0) {
					rawx = event.getHistoricalX(event.getHistorySize()-1) - event.getRawX();
					scene.camera().position.rotateY((float) rawx / 100);
				}
		}
		return false;
	};

	@Override
	protected void onCreateSetContentView()
	{	
		// min3d stuff
		setContentView(R.layout.activity_main);

		LinearLayout ll = (LinearLayout) this.findViewById(R.id.scene1Holder);
		ll.addView(_glSurfaceView);

		textView1 = (TextView) findViewById(R.id.textView1);
		textView2 = (TextView) findViewById(R.id.textView2);
		textView3 = (TextView) findViewById(R.id.textView3);
		textView4 = (TextView) findViewById(R.id.textView4);
		textView5 = (TextView) findViewById(R.id.textView5);
		textView6 = (TextView) findViewById(R.id.textView6);
		textView7 = (TextView) findViewById(R.id.textView7);
		
		btnActive = (ToggleButton) findViewById(R.id.btnActive);

		roboter.init(this);
		mRobotConnection = new Rvm1Connection();

		teachbox.init(this,mRobotConnection);
		
		mBluetoothStuff = new BluetoothUi();
		mBluetoothStuff.init(this);
		
		
		mBluetoothStuff.addCallback(this);	
	}
	

	@Override
	public void initScene()
	{
		IParser parser = Parser.createParser(Parser.Type.OBJ, getResources(), "adv.i31.RobotMin3d:raw/plane_obj", true);
		parser.parse();

		Object3dContainer plane = parser.getParsedObject();

		plane.scale().x = 10.0f;
		plane.scale().y = 10.0f;
		plane.scale().z = 10.0f;
		plane.position().y = -0.3f;
		scene.addChild(plane);

		// add objects to the scene to be rendered
		scene.addChild(roboter);

		scene.backgroundColor().setAll(100, 100, 100, 255);

		scene.camera().position.x = 0;
		scene.camera().position.z = MAX_CAM_X / 2; // 2.5f;
		scene.camera().position.y = 1.0f; // 2.5f;

		Light light = new Light();

		light.position.setAllFrom(scene.camera().position);
		scene.lights().add(light);
	}
	
	@Override
	public void onConnect(BluetoothConnection con)
	{
		// TODO Auto-generated method stub
		mRobotConnection.setConnection(con);
		
		runOnUiThread(new Runnable()
		{
			@Override
			public void run()
			{
				btnActive.setBackgroundColor(Color.GREEN);
			}
		});
		

		
		int i=10;
		while(--i>0) {
			Log.i("gwwg","sending HELLO");
			con.sendString("HELLO\r\n");
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

	@Override
	public void onDisconnect()
	{
		// TODO Auto-generated method stub
		mRobotConnection.setConnection(null);
		
		runOnUiThread(new Runnable()
		{
			@Override
			public void run()
			{
				btnActive.setBackgroundColor(Color.RED);
			}
		});
	}

	@Override
	public void updateScene()
	{
		final double ms_in_a_second = 1000.0;

		long timeMillis = System.currentTimeMillis();
		long timeElapsedMillis = timeMillis - lastTimeMillis;
		lastTimeMillis = timeMillis;

		double x = (double) timeElapsedMillis / ms_in_a_second;

		roboter.update(x);
		
		// camera stuff
		// scene.camera().position.x += camDirection;
		// scene.lights().get(0).position.setAllFrom(scene.camera().position);
		scene.lights().get(0).position.setAllFrom(new Number3d(1.0f, 1.0f, 1.0f));
		
		//scene.lights().get(0).diffuse.setAll((int)(Math.random()*255), (int)(Math.random()*255), (int)(Math.random()*255), 255);

		if (Math.abs(scene.camera().position.x) >= MAX_CAM_X)
			camDirection = -camDirection;
	}
}